package View;

import javax.swing.JFrame;

import java.awt.event.ActionListener;

import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.JButton;

import Entity.CustomerCard;
import Entity.Product;

import java.awt.Font;
import java.io.Serializable;

import javax.swing.JTextArea;
import javax.swing.JCheckBox;
/** this class provide the GUI window for the salesman to update the customer card and history*/
public class UpdateCustomerHistoryView extends JFrame implements Serializable {
	private static final long serialVersionUID = 42L;
	/**required  fields and buttons */
	JLabel lblCust;
	JLabel lblContact;
	JLabel lblProduct;
	JLabel lblCustApp;
	JLabel lblCustumerResponse;
	JLabel lblCall;
	JTextArea txtCustRes;
	JTextField txtCallTime;
	JTextField txtCustomerID;
	JComboBox comboCustomer;
	JCheckBox checkContact;
	JComboBox comboProduct;
	JCheckBox checkApproval;
	JButton UpdateBtn;
	JButton BackBtn;
	
	/** default and only constructor */
	public UpdateCustomerHistoryView() {
		getContentPane().setLayout(null);
		this.setTitle("Update Customer Info");
		this.setBounds(450, 200,436, 418);
		this.setLocationRelativeTo(null); 
		this.setResizable(false);
		
		lblCust = new JLabel("Customer");
		lblCust.setFont(new Font("Tahoma", Font.BOLD, 13));
		lblCust.setBounds(48, 22, 142, 17);
		getContentPane().add(lblCust);
		
		lblContact = new JLabel("Contacted Customer");
		lblContact.setFont(new Font("Tahoma", Font.BOLD, 13));
		lblContact.setBounds(48, 69, 142, 17);
		getContentPane().add(lblContact);	
		
		lblProduct = new JLabel("Product bought");
		lblProduct.setFont(new Font("Tahoma", Font.BOLD, 13));
		lblProduct.setBounds(48, 108, 142, 17);
		getContentPane().add(lblProduct);
		
		lblCustApp = new JLabel("Customer Approval");
		lblCustApp.setFont(new Font("Tahoma", Font.BOLD, 13));
		lblCustApp.setBounds(48, 155, 142, 17);
		getContentPane().add(lblCustApp);
		
		lblCustumerResponse = new JLabel("Customer Response");
		lblCustumerResponse.setFont(new Font("Tahoma", Font.BOLD, 13));
		lblCustumerResponse.setBounds(48, 190, 142, 20);
		getContentPane().add(lblCustumerResponse);
		
		lblCall = new JLabel("Call Time in minutes");
		lblCall.setFont(new Font("Tahoma", Font.BOLD, 13));
		lblCall.setBounds(48, 283, 142, 17);
		getContentPane().add(lblCall);

		txtCustRes = new JTextArea();
		txtCustRes.setBounds(202, 195, 167, 64);
		getContentPane().add(txtCustRes);
		
		
		txtCallTime = new JTextField();
		txtCallTime.setBounds(200, 282, 86, 20);
		getContentPane().add(txtCallTime);
		txtCallTime.setColumns(10);
		
		txtCustomerID = new JTextField();
		txtCustomerID.setBounds(200, 23, 142, 17);
		getContentPane().add(txtCustomerID);
		txtCustomerID.setColumns(10);
		
		
		checkContact = new JCheckBox("");
		checkContact.setBounds(196, 63, 97, 23);
		getContentPane().add(checkContact);
		
		comboProduct = new JComboBox(Product.getProductsStringArr());
		comboProduct.setBounds(200, 109, 142, 17);
		getContentPane().add(comboProduct);
		

		checkApproval = new JCheckBox("");
		checkApproval.setBounds(196, 149, 97, 23);
		getContentPane().add(checkApproval);
		
		UpdateBtn = new JButton("Update");
		UpdateBtn.setFont(new Font("Tahoma", Font.BOLD, 15));
		UpdateBtn.setBounds(239, 330, 135, 28);
		getContentPane().add(UpdateBtn);
		
		BackBtn = new JButton("Back");
		BackBtn.setFont(new Font("Tahoma", Font.BOLD, 15));
		BackBtn.setBounds(48, 330, 135, 28);
		getContentPane().add(BackBtn);
	}
	
	/**
	 * set products function
	 * @param listOfProducts
	 */
	public void setProducts(String[] listOfProducts){
		comboProduct = new JComboBox<String>(listOfProducts);
		comboProduct.addActionListener(comboProduct);
	}
	
	/** show and hide window functions **/
	public void showWindow(){
		setVisible(true);
	}
	public void hideWindow(){
		setVisible(false);
	}
	

	/**
	 * @return button UpdateBtn
	 */ 
	public JButton getUpdateBtn()
	{
		return this.UpdateBtn;
	}
	/**
	 * @return button BackBtn
	 */
	public JButton getBackBtn()
	{
		return this.BackBtn;
	}

	/**
	 * @return lblContact Text
	 */
	public String getCustRes(){
		return txtCustRes.getText();
	}
	
	/**
	 * @return lblCall Text
	 */
	public int getCallTime(){
		return Integer.parseInt(txtCallTime.getText());
	}
	
	
	/**
	 * @return lblCust Text 
	 */
	public String getCustomer(){
		return txtCustomerID.getText();
	}
	
	/**
	 * @return lblContact Text 
	 */
	public String getProduct(){
		return (String) comboProduct.getSelectedItem();
	}
	
	/**
	 * @return lblContact Text 
	 */
	public int getCheckContact(){
		int val;
		 if(checkContact.isSelected()) val=1;
		 else val=0;
		 return val;
	}
	
	
	/**
	 * @return lblCustApp Text 
	 */
	public int getCheckApproval(){
		int val;
		 if(checkApproval.isSelected()) val=1;
		 else val=0;
		 return val;
	}
	
	
	/**clear fields functions */
	public void clearFields(){
		txtCustRes.setText("");
		txtCallTime.setText("");
		comboCustomer.setSelectedItem(" ");
		comboProduct.setSelectedItem(" ");
		checkContact.setSelected(false);
		checkApproval.setSelected(false);
	}
	
	/**
	 * add listeners to the buttons
	 * @param actionListener
	 */
	public void addActionListenerOur(ActionListener actionListener) {
		UpdateBtn.addActionListener(actionListener);
		BackBtn.addActionListener(actionListener);
	}
	
	/**
	 * function that converts from boolean to int for all the checkboxs
	 * @param val
	 * @return
	 */
	public boolean toBoolean(int val){
		if (val==1) return true;
		else return false;
	}
	
	/**
	 * set fields functions
	 * @param data
	 * @param custid
	 */
	public void setDetails(CustomerCard data,String custid) {
		CustomerCard customerCard = data;
		txtCustRes.setText(customerCard.getCustResponse());
		txtCallTime.setText(Integer.toString(customerCard.getCallTime()));
		txtCustomerID.setText(custid);
		txtCustomerID.setEditable(false);
		comboProduct.setSelectedItem(customerCard.getCProductName());
		checkContact.setSelected(toBoolean(customerCard.getCContactCust()));
		checkApproval.setSelected(toBoolean(customerCard.getCustApproval()));
	}
}
